<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="48">
          <a-col :md="8" :sm="24">
            <a-form-item label="项目名称">
              <a-input v-model="queryParam.projectName" placeholder=""/>
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="24">
            <a-form-item label="候选人姓名">
              <a-input v-model="queryParam.candidateName" placeholder=""/>
            </a-form-item>
          </a-col>
          <a-col :md="!advanced && 8 || 24" :sm="24">
            <span class="table-page-search-submitButtons"
                  :style="advanced && { float: 'right', overflow: 'hidden' } || {} ">
              <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
              <a-button style="margin-left: 8px" @click="() => queryParam = {}">重置</a-button>
              <a-button style="margin-left: 8px" @click="downloadExcel()">导出Excel</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <s-table
      ref="table"
      size="default"
      rowKey="key"
      :columns="columns"
      :data="loadData"
      showPagination="auto">
      <span slot="projectType" slot-scope="text">
        {{ text | selectTextFilter1 }}
      </span>
      <span slot="agree" slot-scope="text">
        {{ text | selectTextFilter }}
      </span>
    </s-table>
  </a-card>
</template>

<script>
import { STable } from '@/components'
import { listLogVote, voteExcel } from '@/api/vote/vote'
// 用于处理过滤器动态获取数据
// let that

export default {
  name: 'PointList',
  components: {
    STable,
  },
  filters: {
    selectTextFilter(val) {
      let name = ''
      if (val === 0) {
        name = '同意'
      } else if (val === 1) {
        name = '不同意'
      } else if (val === 2) {
        name = '弃权'
      }
      return name
    },
    selectTextFilter1(val) {
      let name = ''
      if (val === 0) {
        name = '选择评价型'
      } else if (val === 1) {
        name = '同意反对型'
      }
      return name
    },
  },
  data() {
    return {
      agreeStr: ['同意', '不同意', '弃权'],
      // 高级搜索 展开/关闭
      advanced: false,
      // 查询参数
      queryParam: {},
      // 表头
      columns: [
        {
          title: '项目名称',
          dataIndex: 'projectName',
          sorter: true,
          align: 'center'
        },
        {
          title: '投票类型',
          dataIndex: 'projectType',
          scopedSlots: { customRender: 'projectType' },
          sorter: true,
          align: 'center'
        },
        {
          title: '候选人姓名',
          dataIndex: 'candidateName',
          sorter: true,
          align: 'center'
        },
        {
          title: '研究工作水平',
          dataIndex: 'workLevel',
          align: 'center'
        },
        {
          title: '专业技术职务水平',
          dataIndex: 'subLevel',
          align: 'center'
        },
        {
          title: '是否同意',
          dataIndex: 'agree',
          scopedSlots: { customRender: 'agree' },
          align: 'center'
        },
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        // 统一处理查询参数，便于后端表格DTO用固定参数接收
        const queryJson = { 'queryParam': JSON.stringify(this.queryParam) }
        return listLogVote(Object.assign(parameter, queryJson))
          .then(res => {
            return res.result
          })
      },
      selectedRowKeys: [],
      selectedRows: [],

      // custom table alert & rowSelection
      options: {
        alert: {
          show: true,
          clear: () => {
            this.selectedRowKeys = []
          }
        },
        rowSelection: {
          selectedRowKeys: this.selectedRowKeys,
          onChange: this.onSelectChange
        }
      },
      optionAlertShow: false
    }
  },
  beforeCreate() {
    // that = this
  },
  created() {
    this.tableOption()
  },
  methods: {
    tableOption() {
      if (!this.optionAlertShow) {
        this.options = {
          alert: {
            show: true,
            clear: () => {
              this.selectedRowKeys = []
            }
          },
          rowSelection: {
            selectedRowKeys: this.selectedRowKeys,
            onChange: this.onSelectChange
          }
        }
        this.optionAlertShow = true
      } else {
        this.options = {
          alert: false,
          rowSelection: null
        }
        this.optionAlertShow = false
      }
    },

    handleOk() {
      this.$refs.table.clearSelected()
      this.$refs.table.refresh(false)
    },
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    toggleAdvanced() {
      this.advanced = !this.advanced
    },
    downloadExcel() {
      const queryJson = { 'pageNo': 1, 'pageSize': 10000, 'queryParam': JSON.stringify(this.queryParam) }
      voteExcel(Object.assign(queryJson)).then(res => {
        console.log(res)
        const content = res
        const blob = new Blob([content])
        const fileName = '投票日志.xlsx'
        if ('download' in document.createElement('a')) { // 非IE下载
          const elink = document.createElement('a')
          elink.download = fileName
          elink.style.display = 'none'
          elink.href = URL.createObjectURL(blob)
          document.body.appendChild(elink)
          elink.click()
          URL.revokeObjectURL(elink.href) // 释放URL 对象
          document.body.removeChild(elink)
        } else { // IE10+下载
          navigator.msSaveBlob(blob, fileName)
        }
      })
    }
  }
}
</script>
